package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.db.domain.MarketIssueExample;
import com.cskaoyan.market.db.mapper.MarketIssueMapper;
import com.cskaoyan.market.service.MarketIssueService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
@Service
public class MarketIssueServiceImpl implements MarketIssueService {
    @Autowired
    MarketIssueMapper marketIssueMapper;
    @Override
    public List<MarketIssue> readIssueList(Integer page, Integer limit,String question, String sort, String order) {
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        marketIssueExample.orderBy(sort + " " + order);
        MarketIssueExample.Criteria criteria = marketIssueExample.or().andDeletedEqualTo(false);
        if (question!=null && !"".equals(question)){
            System.out.println("like"+question);
            criteria.andQuestionLike("%"+question+"%");
        }
        PageHelper.startPage(page, limit);
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(marketIssueExample);
        return marketIssues;
    }
    @Transactional
    @Override
    public List<MarketIssue> InsertIssue(String question, String answer) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
//        marketIssue.setId(null);
//        marketIssue.setDeleted(true);
        marketIssue.setAddTime(LocalDateTime.now());
        marketIssue.setUpdateTime(LocalDateTime.now());
        int insert = marketIssueMapper.insertSelective(marketIssue);
        System.out.println("insert:"+insert);
        List<MarketIssue> marketIssues = new ArrayList<>();
        if (insert>0){
            marketIssues.add(marketIssue);
            return marketIssues;
        }
        return marketIssues;
    }
    @Transactional
    @Override
    public List<MarketIssue> updateIssue(String id, String question, String answer, String addTime, String updateTime) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        marketIssue.setId(Integer.parseInt(id));
        marketIssue.setAddTime(LocalDateTime.parse( addTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        marketIssue.setUpdateTime(LocalDateTime.now());
        int i = marketIssueMapper.updateByPrimaryKey(marketIssue);
        System.out.println("update:"+i);
        List<MarketIssue> marketIssues = new ArrayList<>();
        if (i>0){
            marketIssues.add(marketIssue);
            return marketIssues;
        }
        return marketIssues;

    }
    @Transactional
    @Override
    public List<MarketIssue> deleteIssue(String id, String question, String answer, String addTime, String updateTime) {
//        SqlSession session = MyBatisUtil.getSession();
//        MarketIssueMapper mapper = session.getMapper(MarketIssueMapper.class);
//        MarketIssue marketIssue = new MarketIssue();
//        int i = mapper.deleteByPrimaryKey(Integer.parseInt(id));
//        System.out.println("delete:"+i);
//        session.commit();
//        session.close();
//        List<MarketIssue> marketIssues = new ArrayList<>();
//        if (i>0){
//            marketIssues.add(marketIssue);
//            return marketIssues;
//        }
//        return marketIssues;
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        marketIssue.setId(Integer.parseInt(id));
        marketIssue.setAddTime(LocalDateTime.parse(addTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        marketIssue.setUpdateTime(LocalDateTime.now());
        marketIssue.setDeleted(true);
        int i = marketIssueMapper.updateByPrimaryKey(marketIssue);
        System.out.println("update:"+i);
        List<MarketIssue> marketIssues = new ArrayList<>();
        if (i>0){
            marketIssues.add(marketIssue);
            return marketIssues;
        }
        return marketIssues;
    }
}
